<script setup lang="ts">
import { getCurrentInstance, ref, reactive, onMounted } from 'vue'
import { onBeforeRouteLeave } from 'vue-router'
const { proxy } = getCurrentInstance()

/* ---- */
const userInfo = proxy.$Store.get('userInfo') || {}
let logUser = proxy.$Store.get('logUser') || {}
const WXConfig = proxy.$Store.get('WXConfig') || {}
const forFees = proxy.$Fees || []

/* --设置微信分享功能-- */
proxy.$WxTool.loadJsdk(WXConfig, (obj) => {
  if (!obj) return
  // ----
  obj.hideMenuItem()
})

/* ---- */
const user = reactive({
  telphone: proxy.$telPhone,
  wechat_img: proxy.$imgEwm,
  // ----
  retailWork: false,
  // ----
  retailPayId: 0,
  retailFeeId: 'marketRent',
  retailFeeVal: 0,
  retailFeeNum: 0,
  retailFeeUnit: '',
  retailFeeUse: true,
  retailTime: '',
  // ----
  forAdm: String(proxy.$Phone) === String(logUser.telphone)
})
// ----
const tbtns = [{
  show: true,
  icon: 'fal fa-house-chimney',
  style: 'TBX-icon xw-radius-round xw-shadow xw-bg-white xw-color-11',
  callback: () => { proxy.$Myfun.openPage(proxy, 'push', 'UcenTer') }
}, {
  show: true,
  icon: 'fal fa-phone-volume',
  style: 'TBX-icon xw-radius-round xw-shadow xw-bg-white xw-color-11',
  callback: () => {
    let options = {
      title: proxy.$Global.layer.titleTwo + user.telphone,
      content: proxy.$Global.layer.conEver,
      button: [{
        text: proxy.$Global.layer.btnCancel
      }, {
        text: proxy.$Global.layer.btnAsk,
        class: 'xw-color-24',
        callback: () => { window.location.href = 'tel:' + user.telphone }
      }]
    }
    if (user.wechat_img) {
      options = {
        title: proxy.$Global.layer.titleFour,
        content: '<div style="padding: 0 2.4rem;"><img src="' + user.wechat_img + '" style="width: 100%;"/></div>',
        button: [{
          text: proxy.$Global.layer.btnCancel
        }, {
          text: proxy.$Global.layer.btnAsk,
          class: 'xw-color-24',
          callback: () => { window.location.href = 'tel:' + user.telphone }
        }]
      }
    }
    proxy.$Myfun.isIndepth(layerBox.value, 'open', options)
  }
}]
// ----
const items = ref([])

/* ---- */
const layer = ref()
const layerBox = ref()

/* ---- */
const setPublicInfo = async (item, callback) => {
  await proxy.$http[item.type](item.url, item.data).then((res) => {
    if (res) callback && callback(res)
  }).catch((err) => { proxy.$Myfun.forToast(layer.value, 'open', err) })
}
const setPayState = async () => {
  if (proxy.$route.query.forToast) proxy.$Myfun.forToast(layer.value, 'open', proxy.$route.query.forToast)
  // ----
  if (proxy.$route.query.forAttr && proxy.$route.query.forVal) {
    const forData = {
      config_id: userInfo.config_id,
      openid: userInfo.openid
    }
    const attrArr = proxy.$route.query.forAttr.split(',')
    const valArr = proxy.$route.query.forVal.split(',')
    attrArr.forEach((item, index) => {
      forData[item] = Number(valArr[index])
    })
    await setPublicInfo({
      type: 'patch',
      url: 'dealer/update?id=' + logUser.id,
      data: forData
    }, (res) => {
      logUser = res
      // --清空参数--
      proxy.$Myfun.openPage(proxy, 'replace', proxy.$route.name)
    })
  }
}
// ----
const getCurrencyInfo = async () => {
  await setPayState()
  // ----
  forFees.some((item) => {
    if (String(item.id) === String(user.retailFeeId)) {
      user.retailFeeVal = Number(item.amount)
      user.retailFeeNum = Number(item.num)
      user.retailFeeUnit = item.typeText
      user.retailFeeUse = item.isOpenUse
    }
  })
  // --获取充值有效期--
  if (user.retailFeeUse) {
    let workLine = []
    let amountDateRetail = 0
    proxy.$Myfun.getJsonData(logUser.topupze, (resJson) => { workLine = resJson })
    workLine.some((oneItem) => {
      if (String(oneItem.id) === String(user.retailFeeId)) {
        oneItem.info.some((twoItem) => {
          if (String(twoItem.id) === String(user.retailPayId)) {
            user.retailTime = twoItem.endtime.split(' ')[0]
            amountDateRetail = Date.parse(new Date(twoItem.endtime.replace(/-/g, '/'))) || 0
          }
        })
      }
    })
    const curDate = Date.parse(new Date())
    user.retailWork = Number(amountDateRetail) >= Number(curDate) 
  } else {
    user.retailWork = Number(logUser.is_retail) === 1
  }
  // ----
  items.value = [{
    show: true,
    child: [{
      show: true,
      isArrow: true,
      name: '门店名称',
      callback: () => { proxy.$Myfun.openPage(proxy, 'push', 'StoreWords') }
    }, {
      show: true,
      isArrow: true,
      name: '门店标牌',
      callback: () => { proxy.$Myfun.openPage(proxy, 'push', 'StoreAvatar') }
    }, {
      show: true,
      isArrow: true,
      name: '商务信息',
      callback: () => { proxy.$Myfun.openPage(proxy, 'push', 'InfoAdm') }
    }, {
      show: true,
      isArrow: true,
      name: '在线客服',
      callback: () => { proxy.$Myfun.openPage(proxy, 'push', 'WechatAdm') }
    }, {
      show: true,
      isArrow: true,
      name: '顶部广告',
      callback: () => { proxy.$Myfun.openPage(proxy, 'push', 'StoreBannerList') }
    }, {
      show: true,
      isArrow: true,
      name: '客户订单',
      callback: () => { proxy.$Myfun.openPage(proxy, 'push', 'ShopOrderOther') }
    }, {
      show: true,
      isArrow: true,
      name: '店二维码',
      callback: () => { proxy.$Myfun.openPage(proxy, 'push', 'GoodsQrCode', { dealer_id: logUser.id }) }
    }, {
      show: true,
      isArrow: true,
      name: '预览分享',
      callback: () => { proxy.$Myfun.openPage(proxy, 'push', 'GoodsHome', { dealer_id: logUser.id }) }
    }]
  }, {
    show: true,
    child: [{
      show: true,
      isArrow: true,
      name: '计量单位',
      callback: () => { proxy.$Myfun.openPage(proxy, 'push', 'GoodsUnit') }
    }, {
      show: true,
      isArrow: true,
      name: '商品分类',
      callback: () => { proxy.$Myfun.openPage(proxy, 'push', 'GoodsCate') }
    }, {
      show: true,
      isArrow: true,
      name: '上传商品',
      callback: () => { proxy.$Myfun.openPage(proxy, 'push', 'GoodsAdm') }
    }, {
      show: true,
      isArrow: true,
      name: '云仓进货',
      callback: () => { proxy.$Myfun.openPage(proxy, 'push', 'CloudShare') }
    }, {
      show: true,
      isArrow: true,
      name: '管理商品',
      callback: () => { proxy.$Myfun.openPage(proxy, 'push', 'GoodsList') }
    }]
  }, {
    show: true,
    child: [{
      show: !user.forAdm,
      isBtn: true,
      isOpen: user.retailWork,
      msg: user.retailWork && user.retailTime && user.retailFeeUse ? '有效期至：' + user.retailTime : '',
      name: '入驻批发市场',
      callback: (item) => {
        const obj = event.target
        if (String(obj.tagName) === 'LABEL') {
          if (!user.retailWork && user.retailFeeUse) {
            const options = {
              type: 6,
              maxCount: 5,
              title: '入驻【万域商友会】批发市场',
              content: '费用：' + user.retailFeeVal.toFixed(2) + '元/' + user.retailFeeNum + user.retailFeeUnit,
              button: [{
                text: proxy.$Global.layer.btnCancel
              }, {
                text: '我来交费',
                class: 'xw-color-24',
                callback: (count) => {
                  layerBox.value.close()
                  // ----
                  const forToPay = { payType: user.retailFeeId, regType: 'jobs', payId: user.retailPayId, payIdKey: 'jobs_id', payCount: count }
                  const forExtend = { name: proxy.$route.name, opts: { forToast: '入驻【万域批发市场】付费成功', forAttr: 'is_retail', forVal: 1 } }
                  proxy.$Myfun.openPage(proxy, 'push', 'PaySys', { toPay: JSON.stringify(forToPay), exTend: JSON.stringify(forExtend) })
                }
              }],
              callback: (count) => {
                layerBox.value.layer.content = '费用：' + Number(user.retailFeeVal * count).toFixed(2) + '元/' + Number(user.retailFeeNum * count) + user.retailFeeUnit
              }
            }
            proxy.$Myfun.isIndepth(layerBox.value, 'open', options)
          } else if (!user.retailFeeUse) {
            let curField = 1
            if (item.isOpen) curField = 0
            // ----
            setPublicInfo({
              type: 'patch',
              url: 'dealer/update?id=' + logUser.id,
              data: {
                is_retail: curField,
                // ----
                config_id: userInfo.config_id,
                openid: userInfo.openid
              }
            }, (res) => { item.isOpen = !item.isOpen })
          }
        }
      }
    }, {
      show: true,
      isArrow: true,
      name: '万域批发市场',
      callback: () => { proxy.$Myfun.openPage(proxy, 'push', 'WholeSale') }
    }]
  }]
}
const getAuditFun = (type, item) => {
  switch (Number(type)) {
    case 1: {
      item.callback && item.callback(item)
      break
    }
  }
}

/* ---- */
onMounted(() => {
  getCurrencyInfo()
})
onBeforeRouteLeave(() => { proxy.$Myfun.setScrollTop(proxy.$refs.formSection) })
</script>

<template>
  <div class="mescroll-touch" ref="formSection">
    <div class="xw-msg xw-bgCL-11"><span>电脑端管理移动门店更便捷：pc.wanyu110.com</span></div>
    <ul class="userSys xw-flex xw-flex-column">
      <template v-for="(item, index) in items">
      <li v-if="item.show">
        <div class="USS-layer xw-bg-gray" v-if="item.layer">{{item.layer}}</div>
        <template v-for="(son, num) in item.child">
        <a class="xw-Tfg xw-flex xw-flex-align" @click="getAuditFun(1, son)" v-if="son.show">
          <div class="USS-title xw-flex-W1">{{son.name}}</div>
          <span v-if="son.msg" v-html="son.msg"></span>
          <div class="USS-check" v-if="son.isBtn"><input type="checkbox" class="a-switch" v-model="son.isOpen"/><label></label></div>
          <i class="fal fa-angle-right" v-if="son.isArrow"></i>
        </a>
        </template>
      </li>
      </template>
    </ul>
    <!---->
    <div class="tabfix xw-visible">
      <template v-for="(item, index) in tbtns">
        <a class="xw-flex xw-flex-center xw-flex-column" :class="item.style" @click="getAuditFun(1, item)" v-if="item.show">
          <em :class="item.icon" v-if="item.icon"></em><span v-html="item.name" v-if="item.name"></span>
        </a>
      </template>
    </div>
    <layer-view ref="layer"/>
    <layer-view ref="layerBox"/>
  </div>
</template>

<style scoped>
/* ---- */
.xw-msg { margin: 0 0 1rem; }
</style>